package com.wuzhazha.javamind.chapter15.test08;

import com.wuzhazha.javamind.chapter15.test04.Generator;

/**
 * 一个通用的生成器，为了减少类型声明，提供了一个泛型方法
 */
public class BasicGenerator<T> implements Generator{

    private Class<T> type;

    public BasicGenerator(Class<T> type){
        this.type = type;
    }

    public T next(){
        try {
            return type.newInstance();
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public static <T> Generator<T> create(Class<T> type){
        return new BasicGenerator<T>(type);
    }
}
